package com.jean.cameratest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.TextureView;

public class MainActivity extends AppCompatActivity {
    private TextureView textureView;
    private Camera1 camera1;
    private Camera2 camera2;
    private final Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = findViewById(R.id.textureView);

        requestPermissions(
                new String[]{
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO
                },
                0x123
        );
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 0x123 && grantResults.length == 2
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    openCamera();
                }
            });
        }
    }

    public void openCamera() {
        if (true) {
            camera1 = new Camera1(this);
            camera1.setTextureView(textureView);
            camera1.open();
        } else {
            camera2 = new Camera2(this);
            camera2.open();
        }
    }
}